package net.hn.hnms.biz.support.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.hn.hnms.biz.support.domain.AbscissdataNowPo;
import net.hn.hnms.biz.support.domain.AnchordataNowPo;
import net.hn.hnms.biz.support.domain.vo.*;
import net.hn.hnms.biz.support.mapper.*;
import net.hn.hnms.biz.support.service.RoofSupprotService;
import net.hn.hnms.biz.support.service.SupportService;
import net.hn.hnms.biz.support.utils.OweDateUtils;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName: RoofSupportController
 * @Description 顶板监测(1014版)
 * @Author 王龙
 * @Date 2022-10-17
 * @Version 0.1
 */
@Api(value = "灾害监测-顶板检测", tags = {"灾害监测-顶板检测（1014）"})
@RestController
@RequestMapping("roofSupport")
public class RoofSupportController {

    /**
     * 顶板监测服务
     */

    @Resource
    private RoofSupprotService roofSupprotService;

    @Resource
    private SupportService supportService;

    @Resource
    private AbscissdataNowMapper abscissdataNowMapper;
    @Resource
    private AnchordataNowMapper anchordataNowMapper;
    @Resource
    private AbscissalarmAbnormalMapper abscissalarmAbnormalMapper;
    @Resource
    private SupportalarmAbnormalMapper supportalarmAbnormalMapper;
    @Resource
    private AnchoralarmAbnormalMapper anchoralarmAbnormalMapper;


    @ApiOperation("统计信息")
    @GetMapping("/statisticalInformation")
    @AutoSetMineCode
    public R<List<StatisticalInformationMapVo>> holderStatisticalInformation(@ApiParam(value = "煤矿编码") String mineCode) {

        RoofStatisticalInformationVo roofStatisticalInformationVo = roofSupprotService.statisticalInformation(mineCode);
        List<StatisticalInformationMapVo> list = new ArrayList<>();
        //最大支架阻力
        StatisticalInformationMapVo maxHolderResistanceVo = new StatisticalInformationMapVo();
        maxHolderResistanceVo.setLable("最大支架阻力");
        maxHolderResistanceVo.setValue(roofStatisticalInformationVo.getMaxHolderResistance());
        maxHolderResistanceVo.setUnint("MPa");
        list.add(maxHolderResistanceVo);

        //最大离层量
        StatisticalInformationMapVo maxAbscissionVo = new StatisticalInformationMapVo();
        maxAbscissionVo.setLable("最大离层量");
        maxAbscissionVo.setValue(roofStatisticalInformationVo.getMaxAbscission());
        maxAbscissionVo.setUnint("mm");
        list.add(maxAbscissionVo);

        //最大锚杆应力
        StatisticalInformationMapVo maxRockBoltVo = new StatisticalInformationMapVo();
        maxRockBoltVo.setLable("最大锚杆应力");
        maxRockBoltVo.setValue(roofStatisticalInformationVo.getMaxRockBolt());
        maxRockBoltVo.setUnint("KN");
        list.add(maxRockBoltVo);

        //最大锚索应力
        StatisticalInformationMapVo maxAnchorCableVo = new StatisticalInformationMapVo();
        maxAnchorCableVo.setLable("最大锚索应力");
        maxAnchorCableVo.setValue(roofStatisticalInformationVo.getMaxAnchorCable());
        maxAnchorCableVo.setUnint("KN");
        list.add(maxAnchorCableVo);

        return R.ok(list);
    }

    @ApiOperation("支架实时数据")
    @GetMapping("/nowSupportData")
    @AutoSetMineCode
    public R<List<HolderResistanceMoveVo>> holderMove(@ApiParam(value = "煤矿编码") String mineCode) {
        List<HolderResistanceMoveVo> list = supportService.selectNowList(mineCode);
        list.forEach(t -> {
            t.setResistanceValue(t.getResistanceValue().setScale(2, BigDecimal.ROUND_HALF_UP));
            t.setMoveValue(t.getMoveValue().setScale(2, BigDecimal.ROUND_HALF_UP));
        });
        return R.ok(list);
    }

    @ApiOperation("离层实时数据")
    @GetMapping("/nowAbscissData")
    @AutoSetMineCode
    public R<List<AbscissdataNowPo>> nowAbscissData(@ApiParam(value = "煤矿编码") String mineCode) {
        List<AbscissdataNowPo> abscissdataNowPos = abscissdataNowMapper.selectList(new LambdaQueryWrapper<AbscissdataNowPo>().eq(StringUtils.isNotBlank(mineCode), AbscissdataNowPo::getMineCode, mineCode));
        abscissdataNowPos.forEach(t -> {
            t.setDeepbasepointValue(t.getDeepbasepointValue().setScale(2, BigDecimal.ROUND_HALF_UP));
            t.setShallowbasepointValue(t.getShallowbasepointValue().setScale(2, BigDecimal.ROUND_HALF_UP));
        });
        return R.ok(abscissdataNowPos);
    }

    @ApiOperation("锚杆索实时数据")
    @GetMapping("/nowAnchordata")
    @AutoSetMineCode
    public R<List<AnchordataNowPo>> nowAnchordata(@ApiParam(value = "煤矿编码") String mineCode) {
        List<AnchordataNowPo> anchordataNowPos = anchordataNowMapper.selectList(new LambdaQueryWrapper<AnchordataNowPo>().eq(StringUtils.isNotBlank(mineCode), AnchordataNowPo::getMineCode, mineCode));
        anchordataNowPos.forEach(t -> {
            t.setPointValue(t.getPointValue().setScale(2, BigDecimal.ROUND_HALF_UP));
        });
        return R.ok(anchordataNowPos);
    }

    @ApiOperation("顶板图表")
    @GetMapping("/echartAbscissVo")
    @AutoSetMineCode
    public R<EchartAbscissVo> echartAbscissVo(@ApiParam(value = "煤矿编码") String mineCode) {
        EchartAbscissVo vo = new EchartAbscissVo();
        abscissdataNowMapper.selectList(new LambdaQueryWrapper<AbscissdataNowPo>().eq(StringUtils.isNotBlank(mineCode), AbscissdataNowPo::getMineCode, mineCode)).forEach(s -> {
            vo.getXdate().add(s.getPointLocation());
            vo.getDeeps().add(s.getDeepbasepointValue());
            vo.getShallow().add(s.getShallowbasepointValue());
        });
        return R.ok(vo);
    }

    @ApiOperation("工作面选择")
    @GetMapping("/selectFace")
    @AutoSetMineCode
    public R<Set<String>> selectFace(@Param("mineCode") String mineCode) {
        Set<String> set = new HashSet<>();
        anchordataNowMapper.selectList(new LambdaQueryWrapper<AnchordataNowPo>().eq(StringUtils.isNotBlank(mineCode), AnchordataNowPo::getMineCode, mineCode)).forEach(s -> {
            set.add(s.getPointLocation().substring(0, 6));
        });
        return R.ok(set);
    }

    @ApiOperation("锚杆索图表")
    @GetMapping("/echartAnchorVo")
    @AutoSetMineCode
    public R<EchartAnchorVo> echartAnchorVo(@ApiParam(value = "工作面", required = true) String faceCode,
                                            @ApiParam(value = "锚杆或锚索", required = true) String type,
                                            @Param("mineCode") String mineCode) {
        EchartAnchorVo vo = new EchartAnchorVo();
        LambdaQueryWrapper<AnchordataNowPo> objectLambdaQueryWrapper = new LambdaQueryWrapper<AnchordataNowPo>();
        if (StringUtils.isNotBlank(faceCode)) {
            objectLambdaQueryWrapper.likeRight(AnchordataNowPo::getPointLocation, faceCode);
        }
        if (StringUtils.isNotBlank(type)) {
            objectLambdaQueryWrapper.like(AnchordataNowPo::getPointLocation, type);
        }
        objectLambdaQueryWrapper.eq(StringUtils.isNotBlank(mineCode), AnchordataNowPo::getMineCode, mineCode);
        anchordataNowMapper.selectList(objectLambdaQueryWrapper).forEach(s -> {
            vo.getXdate().add(s.getPointLocation());
            vo.getValues().add(s.getPointValue());
        });
        return R.ok(vo);
    }

    @ApiOperation("工作面选择")
    @GetMapping("/alarmVo")
    @AutoSetMineCode
    public R<AlarmVo> alarm(@Param("mineCode") String mineCode) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        AlarmVo vo = new AlarmVo();
        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(mineCode)) {
            wrapper.eq("mine_code", mineCode);
        }
        wrapper.gt("data_time", calendar.getTime());
        vo.setAbsciss(abscissalarmAbnormalMapper.selectCount(wrapper));
        vo.setSupport(supportalarmAbnormalMapper.selectCount(wrapper));
        QueryWrapper mgWrapper = new QueryWrapper();
        mgWrapper.gt("data_time", calendar.getTime());
        mgWrapper.like("point_location", "锚杆");
        vo.setMaogan(anchoralarmAbnormalMapper.selectCount(mgWrapper));
        QueryWrapper msWrapper = new QueryWrapper();
        msWrapper.gt("data_time", calendar.getTime());
        msWrapper.like("point_location", "锚索");
        vo.setMaosuo(anchoralarmAbnormalMapper.selectCount(msWrapper));
        return R.ok(vo);
    }


    @ApiOperation("顶板灾害--数据统计")
    @GetMapping("/supportStatistical")
    @AutoSetMineCode
    public R<SupportAlarmAnalysisVo> statisticalInformation(@ApiParam(value = "煤矿编码") String mineCode) {
        SupportAlarmAnalysisVo supportAlarmAnalysisVo = new SupportAlarmAnalysisVo();
        List<ChartAnaliseVo> chart = new ArrayList<>();
        String endTime = DateUtils.getDate();
        String startTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.addDays(DateUtils.getNowDate(), -7));
        List<String> day = OweDateUtils.getXData(startTime, endTime, "day");
        List<ChartAnaliseVo> chartData = supportalarmAbnormalMapper.getSupportStatistical(mineCode, startTime, endTime);
        day.forEach(item -> {
            ChartAnaliseVo chartAnaliseVo = chartData.stream().filter(t -> t.getX().contains(item)).findFirst().orElse(new ChartAnaliseVo(item, "0", ""));
            chart.add(chartAnaliseVo);
        });
        ChartAnaliseVo chartAnaliseVo = chart.get(chart.size() - 1);
        supportAlarmAnalysisVo.setAlarmCount(chartAnaliseVo.getY());
        supportAlarmAnalysisVo.setChartData(chart);
        supportAlarmAnalysisVo.setHolderPassRate(Integer.parseInt(chartAnaliseVo.getY()) > 0 ? "95%" : "0");
        return R.ok(supportAlarmAnalysisVo);
    }


}
